//
//  CustomTabBarViewController.m
//  Xinjuxin
//
//  Created by 廖挺 on 16/5/31.
//  Copyright © 2016年 liaoting. All rights reserved.
//

#import "CustomTabBarViewController.h"
#import "CustomViewController.h"

static const int CountTabBar = 4;

@interface CustomTabBarViewController () <UITabBarControllerDelegate>

@end

@implementation CustomTabBarViewController

- (UIImage*)createImageWithColor:(UIColor*)color {
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

- (void)viewDidLoad {
    NSArray *arrayTitle = @[@"首页",@"分类",@"购物车",@"我的"];
    NSArray *arrayVcName = @[@"FirstViewController",@"SecondViewController",@"ThirdViewController",@"ForthViewController",];
    UIImage *imageTop = [self createImageWithColor:[UIColor colorWithHexString:@"#91C25F"]];
    NSArray *arrayImageName = @[@"home",@"classify",@"shoppingcart",@"mine"];
    for (NSInteger index = 0; index < CountTabBar; index++) {
        NSString *title = arrayTitle[index];
        NSString *imageName = arrayImageName[index];
        NSString *deSelectImage = [NSString stringWithFormat:@"tabbar_%@_normal",imageName];
        NSString *selectImage = [NSString stringWithFormat:@"tabbar_%@_highlight",imageName];
        Class class = NSClassFromString(arrayVcName[index]);
        CustomViewController *viewController = [[class alloc] init];
        viewController.tabBarItem.title = title;
        [viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#323232"]} forState:UIControlStateNormal];
        [viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#91C25F"]} forState:UIControlStateSelected];
        viewController.view.backgroundColor = [UIColor whiteColor];
        [viewController.tabBarItem setImage:[[UIImage imageNamed:deSelectImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        [viewController.tabBarItem setSelectedImage:[[UIImage imageNamed:selectImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        viewController.tabBarItem.badgeValue = @"5";
        viewController.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -3);
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
        navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:19]};
        [navigationController.navigationBar setBackgroundImage:[imageTop resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault];
        [self addChildViewController:navigationController];
    }
    self.delegate = self;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
{
    

}


@end
